package com.baizhi.shane.shane.service.impl;

import com.baizhi.shane.constant.SysMapping;
import com.baizhi.shane.shane.entity.ShaneCollectQuestion;
import com.baizhi.shane.shane.vo.QuestionUserRelVO;
import com.baizhi.shane.sys.manager.RedisManager;
import com.baizhi.shane.sys.utils.CommonUtils;
import com.baizhi.shane.sys.vo.IdVO;
import com.baizhi.shane.sys.vo.SuccessedOutVO;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baizhi.shane.shane.entity.ShaneCollectQuestion;
import com.baizhi.shane.shane.service.ShaneCollectQuestionService;
import com.baizhi.shane.shane.mapper.ShaneCollectQuestionMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
* @author wbz12
* @description 针对表【shane_collect_question(啥呢问题收藏表)】的数据库操作Service实现
* @createDate 2023-07-25 22:33:37
*/
@Service
public class ShaneCollectQuestionServiceImpl extends ServiceImpl<ShaneCollectQuestionMapper, ShaneCollectQuestion>
    implements ShaneCollectQuestionService{

    @Resource
    private RedisManager redisManager;
    @Resource
    private CommonUtils commonUtils;

    /**
     * 用户收藏问题，只需要questionId即可，userId自动分配为当前用户
     *
     * @Author 闻柏智
     * @Date 19:01 2023/10/4
     * @Param questionUserRelVO
     * @return java.lang.String
     **/
    @Override
    public String insertOneQuestionCollect(QuestionUserRelVO questionUserRelVO) {
        Long userId = redisManager.getUserId();
        Long questionId = questionUserRelVO.getQuestionId();
        ShaneCollectQuestion shaneCollectQuestion = new ShaneCollectQuestion();
        shaneCollectQuestion.setUserId(userId);
        shaneCollectQuestion.setQuestionId(questionId);
        boolean save = this.save(shaneCollectQuestion);
        String succeed = commonUtils.boolToString(save, SysMapping.COLLECT_SUCCESS, SysMapping.COLLECT_FAIL);
        return succeed;
    }

    /**
     * 用户取消收藏问题，只需要questionId即可，userId自动分配为当前用户
     *
     * @Author 闻柏智
     * @Date 19:15 2023/10/4
     * @Param questionUserRelVO
     * @return java.lang.String
     **/
    @Override
    public String deleteOneQuestionCollect(QuestionUserRelVO questionUserRelVO) {
        Long userId = redisManager.getUserId();
        Long questionId = questionUserRelVO.getQuestionId();

        LambdaUpdateWrapper<ShaneCollectQuestion> uwScq = new LambdaUpdateWrapper<>();
        uwScq.eq(ShaneCollectQuestion::getUserId,userId);
        uwScq.eq(ShaneCollectQuestion::getQuestionId,questionId);
        uwScq.eq(ShaneCollectQuestion::getDelFlag,0);
        uwScq.set(ShaneCollectQuestion::getDelFlag, 1);
        uwScq.set(ShaneCollectQuestion::getDelBy, userId);
        uwScq.set(ShaneCollectQuestion::getDelTime, new Date());
        boolean update = this.update(uwScq);
        String succeed = commonUtils.boolToString(update, SysMapping.COLLECT_CANCEL_SUCCESS, SysMapping.COLLECT_CANCEL_FAIL);
        return succeed;
    }

}




